package com.puyuba.puyuba.utils.custommessage;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMCustomMessageBody;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.modules.chat.EaseChatFragment;
import com.hyphenate.easeui.modules.chat.EaseChatInputMenu;
import com.hyphenate.easeui.modules.chat.EaseChatMessageListLayout;
import com.hyphenate.easeui.modules.chat.interfaces.IChatExtendMenu;
import com.hyphenate.easeui.modules.chat.interfaces.IChatPrimaryMenu;
import com.puyuba.puyuba.network.Constants;
import com.pyb.java.commonutil.ToastHelper;

import com.pyb.java.cache.CacheUserInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * 聊天页面Fragment
 */
public class ChatFragment extends EaseChatFragment {
    private EMMessage customMessage;
    private EMMessage submitneedMessage;

    @Override
    public void initView() {
        super.initView();
        //获取到聊天列表控件
        EaseChatMessageListLayout messageListLayout = chatLayout.getChatMessageListLayout();
        //设置聊天列表背景
        messageListLayout.setBackground(new ColorDrawable(Color.parseColor("#F5F5F5")));
        setview();
    }

    //点击头像
    @Override
    public void onUserAvatarClick(String username) {
        super.onUserAvatarClick(username);
        ToastHelper.show(username);
    }

    //隐藏聊天扩展区其他item
    private void setview() {
        //获取到菜单输入父控件
        EaseChatInputMenu chatInputMenu = chatLayout.getChatInputMenu();
        //获取到菜单输入控件
        IChatPrimaryMenu primaryMenu = chatInputMenu.getPrimaryMenu();
        //获取到扩展区域控件
        IChatExtendMenu chatExtendMenu = chatInputMenu.getChatExtendMenu();

        chatExtendMenu.clear();
        chatExtendMenu.registerMenuItem(R.string.attach_picture, R.drawable.ease_chat_image_selector, R.id.extend_item_picture);
        chatExtendMenu.registerMenuItem(R.string.attach_take_pic, R.drawable.ease_chat_takepic_selector, R.id.extend_item_take_picture);
        chatExtendMenu.registerMenuItem(R.string.attach_file, R.drawable.em_chat_file_selector, R.id.extend_item_file);
    }

    //发送交换电话弹窗
    public void sendPhoneMsg(String user, String time,String type){
        if (customMessage == null){
        }
        customMessage = EMMessage.createSendMessage(EMMessage.Type.CUSTOM);
        customMessage.setMessageStatusCallback(new EMCallBack() {
            @Override
            public void onSuccess() {
//                chatLayout.deleteMessage(customMessage);
                deleteMsg(customMessage);
            }

            @Override
            public void onError(int code, String error) { }

            @Override
            public void onProgress(int progress, String status) { }
        });
        // event为需要传递的自定义消息事件，比如礼物消息，可以设置event = "gift"
        EMCustomMessageBody body = new EMCustomMessageBody(Constants.MESSAGE_ATTR_IS_CUSTOM_NEW);
        // params类型为Map<String, String>
        Map<String,String> params = new HashMap<>();
        params.put(Constants.TYPE,type);
        params.put(Constants.USER_NAME,user);
        params.put(Constants.TIME,time);
        params.put(Constants.RESULT,"none");
        if (type.equals("phone")){
            params.put(Constants.PHONE, CacheUserInfo.getInstance().getsUserInfo()
                    .getUser().getMobile());
        }
        body.setParams(params);
        customMessage.addBody(body);

        // to指另一方环信id（或者群组id，聊天室id）
        customMessage.setTo(conversationId);
        EMClient.getInstance().chatManager().sendMessage(customMessage);
//        chatFragment.chatLayout.sendMessage(customMessage);

    }


    //需求展示弹窗
    public void submitneed(int type,String age,String job,String company,String industry,String now_edu,String aim_edu,String year,int diff_level){
        submitneedMessage = EMMessage.createSendMessage(EMMessage.Type.CUSTOM);

        EMCustomMessageBody submitbody = new EMCustomMessageBody(Constants.DEMAND_INFO_CARD_CUSTOM_MSG);
        // params类型为Map<String, String>
        Map<String,String> params = new HashMap<>();
        params.put(Constants.TYPE,type==1 ? "zhiye" : "xueli");;
        if (type == 1){
            //职业
            params.put(Constants.AGE,age);
            params.put(Constants.JOB,job);
            params.put(Constants.COMPANY,company);
            params.put(Constants.INDUSTRY,industry);
            params.put(Constants.YEAR,year);
        }else {
            params.put(Constants.NOW_EDU,now_edu);
            params.put(Constants.AIM_EDU,aim_edu);
            params.put(Constants.YEAR,year);
            params.put(Constants.DIFF_LEVEL,diff_level==1 ? "简单" : "较难");
        }
        submitbody.setParams(params);
        submitneedMessage.addBody(submitbody);

        // to指另一方环信id（或者群组id，聊天室id）
        submitneedMessage.setTo(conversationId);
        EMClient.getInstance().chatManager().sendMessage(submitneedMessage);
//        chatFragment.chatLayout.sendMessage(submitneedMessage);

    }
    //删除当前会话的某条聊天记录
    private void deleteMsg(EMMessage message){
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(conversationId);
        conversation.removeMessage(message.getMsgId());
    }
    //根据会话插入消息
    private void insertMsg(EMMessage msg){
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(conversationId);
        conversation.insertMessage(msg);
    }

    @Override
    public void onChatSuccess(EMMessage message) {
        super.onChatSuccess(message);
//        deleteMsg(customMessage);
    }


}
